﻿Imports Microsoft.VisualBasic


Public Class BattleshipGame
    Private m_NumPlayers As Integer
    Private m_Player1Name As String
    Private m_Player2Name As String
    Private m_Player1Wins As Integer
    Private m_Player2Wins As Integer
    Private m_Player1Losses As Integer
    Private m_Player2Losses As Integer
    Private m_Player1Lives As Integer 'counts down to zero, and then you're dead
    Private m_Player2Lives As Integer
    Private m_Player1FiringGrid As New BattleshipGameGrid(1, False, True)
    Private m_Player1TrackingGrid As New BattleshipGameGrid(1, True, False)
    Private m_Player2FiringGrid As New BattleshipGameGrid(2, False, True)
    Private m_Player2TrackingGrid As New BattleshipGameGrid(2, True, False)
    Public Player1_Ships(5) As Ship
    Public Player2_Ships(5) As Ship
    'Temporal properties
    Dim m_currentphase As Integer
    Dim m_currentplayerturn As Integer

    Public Property NumPlayers() As Integer
        Get
            Return m_NumPlayers
        End Get
        Set(ByVal value As Integer)
            m_NumPlayers = value
        End Set
    End Property

    'Phase 1 properties
    Dim m_player1_shipstoplace As Integer
    Dim m_player2_shipstoplace As Integer


    Public Property Player1Name() As String
        Get
            Return m_Player1Name
        End Get
        Set(ByVal value As String)
            m_Player1Name = value
        End Set
    End Property

    Public Property Player2Name() As String
        Get
            Return m_Player2Name
        End Get
        Set(ByVal value As String)
            m_Player2Name = value
        End Set
    End Property

    Public Property Player1Wins() As Integer
        Get
            Return m_Player1Wins
        End Get
        Set(ByVal value As Integer)
            m_Player1Wins = value
        End Set
    End Property

    Public Property Player2Wins() As Integer
        Get
            Return m_Player2Wins
        End Get
        Set(ByVal value As Integer)
            m_Player2Wins = value
        End Set
    End Property

    Public Property Player1Losses() As Integer
        Get
            Return m_Player1Losses
        End Get
        Set(ByVal value As Integer)
            m_Player1Losses = value
        End Set
    End Property

    Public Property Player2Losses() As Integer
        Get
            Return m_Player2Losses
        End Get
        Set(ByVal value As Integer)
            m_Player2Losses = value
        End Set
    End Property

    Public Property Player1Lives() As Integer
        Get
            Return m_Player1Lives
        End Get
        Set(ByVal value As Integer)
            m_Player1Lives = value
        End Set
    End Property

    Public Property Player2Lives() As Integer
        Get
            Return m_Player2Lives
        End Get
        Set(ByVal value As Integer)
            m_Player2Lives = value
        End Set
    End Property

    Public Property Player1FiringGrid() As BattleshipGameGrid
        Get
            Return m_Player1FiringGrid
        End Get
        Set(ByVal value As BattleshipGameGrid)
            m_Player1FiringGrid = value
        End Set
    End Property

    Public Property Player1TrackingGrid() As BattleshipGameGrid
        Get
            Return m_Player1TrackingGrid
        End Get
        Set(ByVal value As BattleshipGameGrid)
            m_Player1TrackingGrid = value
        End Set
    End Property

    Public Property Player2FiringGrid() As BattleshipGameGrid
        Get
            Return m_Player2FiringGrid
        End Get
        Set(ByVal value As BattleshipGameGrid)
            m_Player2FiringGrid = value
        End Set
    End Property
    Public Property Player2TrackingGrid() As BattleshipGameGrid
        Get
            Return m_Player2TrackingGrid
        End Get
        Set(ByVal value As BattleshipGameGrid)
            m_Player2TrackingGrid = value
        End Set
    End Property

    Public Property CurrentPlayerTurn() As Integer
        Get
            Return m_currentplayerturn
        End Get
        Set(ByVal value As Integer)
            m_currentplayerturn = value
        End Set
    End Property

    Public Property CurrentPhase As Integer
        Get
            Return m_currentphase
        End Get
        Set(ByVal value As Integer)
            m_currentphase = value
        End Set
    End Property


    Public Property Player1_ShipstoPlace As Integer
        Get
            Return m_player1_shipstoplace
        End Get
        Set(ByVal value As Integer)
            m_player1_shipstoplace = value
        End Set
    End Property


    Public Property Player2_ShipstoPlace As Integer
        Get
            Return m_player2_shipstoplace
        End Get
        Set(ByVal value As Integer)
            m_player2_shipstoplace = value
        End Set
    End Property


    Public Sub New()
        Player1_Ships(5) = Nothing
        Player2_Ships(5) = Nothing

        Dim Player1Tracking As New BattleshipGameGrid(1, True, False)
        Dim Player1Firing As New BattleshipGameGrid(1, False, True)
        Dim Player2Tracking As New BattleshipGameGrid(2, True, False)
        Dim Player2Firing As New BattleshipGameGrid(2, False, True)

        m_Player1TrackingGrid = Player1Tracking
        m_Player1FiringGrid = Player1Firing
        m_Player2TrackingGrid = Player2Tracking
        m_Player2FiringGrid = Player2Firing

        CurrentPhase = 1
        CurrentPlayerTurn = 1

        Player1_ShipstoPlace = 5
        Player2_ShipstoPlace = 5

        Player1Lives = 17
        Player2Lives = 17

        FillShipsDataStructure(1)
        FillShipsDataStructure(2)

    End Sub

    Function FillShipsDataStructure(ByVal whichplayer As Integer) As Boolean
        'I hard-coded these in to comply with basic battleship rules. 

        Dim carrier As New Ship(5)
        Dim battleship As New Ship(4)
        Dim cruiser As New Ship(3)
        ' Dim submarine As New Ship(3) 'having two of the same size with different sprites would of taken too long, in retrospect.
        Dim destroyer As New Ship(2)

        If whichplayer = 1 Then

            Player1_Ships(1) = destroyer
            Player1_Ships(2) = cruiser
            Player1_Ships(3) = cruiser 'not a sub, gimme a break.
            Player1_Ships(4) = battleship
            Player1_Ships(5) = carrier


            Return True
        End If


        If whichplayer = 2 Then


            Player2_Ships(1) = destroyer
            Player2_Ships(2) = cruiser
            Player2_Ships(3) = cruiser
            Player2_Ships(4) = battleship
            Player2_Ships(5) = carrier

            Return True
        End If


        Return False

    End Function


    'Turn Management Starts with setting phase and player. 
    'Each player sets all of thier ships before the next one goes. 

    'check win conditions - true for win, false for no win yet
    Public Function CheckWinConditions() As Boolean
        If Me.Player1Lives <= 0 Then
            Me.Player2Wins += 1
            Me.Player1Losses += 1
            MsgBox("Game over! Player 2 wins.")
            Return True
        ElseIf Me.Player2Lives <= 0 Then
            Me.Player1Wins += 1
            Me.Player2Losses += 1
            MsgBox("Game over! Player 1 wins.")
            Return True
        End If
        Return False
    End Function

End Class
